﻿using System;
using System.Collections.Generic;
using System.IO;
using FluentFTP.Configuration;
using FluentFTP.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace FluentFTPTest
{
    [TestClass]
    public class SettingsTest
    {
        const string password = "pass";
        const string userName = "anonymous";
        const string defaultSaveFolder = "C:\\test";

        [TestMethod]
        public void SaveUserSettingsTest()
        {
            setUpSettings();

            Assert.IsTrue(File.Exists(Settings.UserSettingsPath));
            Assert.IsTrue((new FileInfo(Settings.UserSettingsPath)).Length > 0);
        }

        [TestMethod]
        public void LoadUserSettingsTest()
        {
            setUpSettings();

            Settings.Reload();

            Assert.AreEqual(password, Settings.UserSettings.DefaultPassword);
            Assert.AreEqual(userName, Settings.UserSettings.DefaultUserName);
            Assert.AreEqual(defaultSaveFolder, Settings.UserSettings.DefaultSaveFolder);
        }

        [TestMethod]
        public void SaveServersTest()
        {
            setUpSettings();

            Assert.IsTrue(File.Exists(Settings.ServersPath));
            Assert.IsTrue((new FileInfo(Settings.ServersPath)).Length > 0);
        }

        [TestMethod]
        public void LoadServersTest()
        {
            setUpSettings();

            Settings.Reload();

            Assert.AreEqual(2, Settings.Servers.Count);
            Assert.IsTrue(Settings.Servers.Any(x => x.Name == "test1" && x.Adress == "ftp://127.0.0.1" && x.Password == "pass" && x.UserName == "user1"));
        }

        private static void setUpSettings()
        {
            Settings.UserSettings.DefaultPassword = password;
            Settings.UserSettings.DefaultUserName = userName;
            Settings.UserSettings.DefaultSaveFolder = defaultSaveFolder;

            Settings.Servers.Clear();

            Settings.Servers.Add(new FtpServerInfo { Name = "test1", Adress = "ftp://127.0.0.1", Password = "pass", UserName = "user1" });
            Settings.Servers.Add(new FtpServerInfo { Name = "test2", Adress = "ftp://127.0.0.2", Password = "pass2", UserName = "user2" });

            Settings.Save();
        }
    }
}
